package com.wn;

import com.wn.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2023 -03-03
 * @desc
 *      操作  索引库
 */
@SpringBootTest
public class ESTest01 {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
    public void demo01(){

        //拿到索引配置
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Student.class);

        //创建索引
        indexOperations.create();

        //删除
//        indexOperations.delete()

    }

    /**
     * 添加数据
     */
    @Test
    public void addStu(){

        Student student = new Student();
        student.setId("1");
        student.setName("张无忌");
        student.setDesc("xx");
        student.setData("xx");
        student.setAge(20);

        elasticsearchRestTemplate.save(student);

    }

    /**
     * 添加数据
     */
    @Test
    public void addStus(){

        List<Student> list = new ArrayList<>();
        list.add(new Student("2","李四","苹果手机","1",22));
        list.add(new Student("3","王五","oppo手机","2",24));
        list.add(new Student("4","赵六","voio手机","3",25));
        list.add(new Student("5","田七","小米手机","4",26));
        elasticsearchRestTemplate.save(list);

    }
}
